﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Cashier.utils;
using Cashier_Model;
using Cashier_Service;

namespace Cashier.rights
{
    public partial class RoleAddForm : Form
    {
        private RightsForm rightsform;
        private Role role;
        private RoleService roleService;
        private bool isSave = false;
        private long roleid = 0;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        /// <param name="rightsform"></param>
        /// <param name="roleid"></param>
        public RoleAddForm(MainForm mainform, RightsForm rightsform, long roleid)
        {
            InitializeComponent();
            this.rightsform = rightsform;
            this.roleService = new RoleService();
            this.roleid = roleid;
            if (roleid > 0)
            {
                this.InitialFormData(roleid);
                button_save.Tag = "role_edit";
            }
            else
                button_save.Tag = "role_add";
            Button[] buttons = { button_save };
            RightsforControls.RightsforButton(mainform.Admin.RolePurviewtable, buttons);
        }

        #region 功能方法实现

        /// <summary>
        /// 初始化表单数据
        /// </summary>
        private void InitialFormData(long roleid)
        {
            role = this.roleService.GetRole(roleid);
            if (role != null)
                FormUtil.InitialFormData(role, this, 1);
        }

        /// <summary>
        /// 保存角色
        /// </summary>
        private void SaveRole()
        {
            ArrayList list = new ArrayList();
            FormUtil.GetValidateControlsNotPassed(this, list, 1);
            if (list.Count > 0)
                return;

            if (role == null)
                role = new Role();
            FormUtil.GetFormData(role, this, 1);

            if(roleService.ExistSameRolename(roleid, textbox_name.Text.Trim()))
            {
                MessageBox.Show("角色名称重复！");
                return;
            }

            bool issuccess = false;
            if (role.Id > 0)
            {
                int flag = this.roleService.UpdateRole(role);
                if (flag > 0)
                    issuccess = true;
            }
            else
            {
                long id = this.roleService.InsertRole(role);
                if (id > 0)
                {
                    role.Id = id;
                    issuccess = true;
                }
            }

            if (issuccess)
            {
                DialogResult result = MessageBox.Show("保存成功，是否添加新记录！", "提示", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    role = null;
                    FormUtil.ClearFormData(this, 1);
                }
                else
                {
                    this.rightsform.InitialRoleListBox();
                    this.isSave = false;
                    this.Close();
                }
            }
        }

        #endregion

        #region 控件事件

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_save_Click(object sender, EventArgs e)
        {
            this.SaveRole();
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 窗体关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RoleAddForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isSave)
            {
                this.SaveRole();
            }
        }

        #endregion

        #region 方法重写

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Escape:
                    isSave = false;
                    this.Close();
                    break;
                case Keys.F5:
                    this.SaveRole();
                    break;
            }
            return b;
        }

        #endregion

    }
}
